package com.lanchewuyou.user.driver.lipeiui;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.lanchewuyou.user.driver.R;
import com.lanchewuyou.user.driver.modul.Config;
import com.lanchewuyou.user.driver.modul.OkHttpClientManager;
import com.rengwuxian.materialedittext.MaterialEditText;
import com.squareup.okhttp.Request;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

/**
 * 收集油位，车牌号，车型信息,公里数页面
 */

public class CarInforActivity extends AppCompatActivity implements View.OnClickListener {
    private MaterialEditText carKind, carDistance;
    private TextView carNumber;
    private Toolbar mToolbar;
    private Button carInforNextBtn;
    private Button gasBtn1, gasBtn2, gasBtn3, gasBtn4, gasBtn5, gasBtn6, gasBtn7, gasBtn8, gasBtn9, gasBtn10;
    private int gasNum = 0;
    private ImageView youzhenImg;
    private RotateAnimation allAnimation;
    private float point = -65f;
    public static CarInforActivity instance;
    private ProgressDialog progressDialog, progressUploadDialog;
    private static String DRIVERID;

    private Toast toast = null;

    private void showTextToast(String msg) {
        if (toast == null) {
            toast = Toast.makeText(CarInforActivity.this, msg, Toast.LENGTH_SHORT);
        } else {
            toast.setText(msg);
        }
        toast.show();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_car_infor);
        initViews();
        progressDialog.show();
        instance = this;
        SharedPreferences sharedPreferences = getSharedPreferences(Config.PASSWORD_PATH, Activity.MODE_PRIVATE);
        DRIVERID = sharedPreferences.getString("driverid", "");
        OkHttpClientManager.getAsyn(Config.DRIVERORDERDETAIL + "&driverid=" + DRIVERID + "&orderid=" + PickCarActivity.orderId, new OkHttpClientManager.StringCallback() {
            @Override
            public void onFailure(Request request, IOException e) {
                showTextToast("没有获取到详情");
                progressDialog.dismiss();
            }

            @Override
            public void onResponse(String response) {
                Log.d("Pickcar", response);
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    String status = jsonObject.getString("status");
                    if (status.equals("1")) {
                        JSONObject info = jsonObject.getJSONObject("info");
                        carNumber.setText(info.getString("chepai"));
                        String gasString = info.getJSONObject("carinfo").getString("oilinfo");
                        gasNum = Integer.valueOf(gasString);
                        setButtonred(gasNum - 1);
                        allAnimation = new RotateAnimation(point, (gasNum - 5) * 13f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 1.0f);
                        allAnimation.setFillAfter(true);
                        allAnimation.setDuration(1500);
                        youzhenImg.startAnimation(allAnimation);
                        carKind.setText(info.getJSONObject("carinfo").getString("carmodels"));
                        carDistance.setText(info.getJSONObject("carinfo").getString("carmileage"));
                        progressDialog.dismiss();
                    } else {
                        showTextToast("没有获取到详情");
                        progressDialog.dismiss();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                    progressDialog.dismiss();
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.gasbtn1:
                setbuttonAnima(1);
                break;
            case R.id.gasbtn2:
                setbuttonAnima(2);
                break;
            case R.id.gasbtn3:
                setbuttonAnima(3);
                break;
            case R.id.gasbtn4:
                setbuttonAnima(4);
                break;
            case R.id.gasbtn5:
                setbuttonAnima(5);
                break;
            case R.id.gasbtn6:
                setbuttonAnima(6);
                break;
            case R.id.gasbtn7:
                setbuttonAnima(7);
                break;
            case R.id.gasbtn8:
                setbuttonAnima(8);
                break;
            case R.id.gasbtn9:
                setbuttonAnima(9);
                break;
            case R.id.gasbtn10:
                setbuttonAnima(10);
                break;
            case R.id.car_infor_next_btn:
                progressUploadDialog.show();
                Log.d("carinfor", Config.CARINFOR + PickCarActivity.orderId + "&driverid=" + DRIVERID +
                        "&oilinfo=" + gasNum + "&carmodels=" + carKind.getText().toString() + "&carmileage=" + carDistance.getText().toString());
                if (gasNum == 0 || TextUtils.isEmpty(carKind.getText().toString()) || TextUtils.isEmpty(carDistance.getText().toString())) {
                    showTextToast("有信息未完善");
                    progressUploadDialog.dismiss();
                } else {
                    OkHttpClientManager.getAsyn(Config.CARINFOR + PickCarActivity.orderId + "&driverid=" + DRIVERID +
                                    "&oilinfo=" + gasNum + "&carmodels=" + carKind.getText().toString() + "&carmileage=" + carDistance.getText().toString(),
                            new OkHttpClientManager.StringCallback() {
                                @Override
                                public void onFailure(Request request, IOException e) {
                                    progressUploadDialog.dismiss();
                                    showTextToast("服务器出了点问题");
                                }

                                @Override
                                public void onResponse(String response) {
                                    try {
                                        JSONObject jsonObject = new JSONObject(response);
                                        String status = jsonObject.getString("status");
                                        if (status.equals("1")) {
                                            Intent intent = new Intent(CarInforActivity.this, CarfixInforActivity.class);
                                            startActivity(intent);
                                            progressUploadDialog.dismiss();
                                            showTextToast("上传成功");
                                        } else {
                                            progressUploadDialog.dismiss();
                                            showTextToast("请求失败");
                                        }
                                    } catch (JSONException e) {
                                        e.printStackTrace();
                                        progressUploadDialog.dismiss();
                                    }
                                }
                            });
                }
                break;
            default:
                break;
        }
    }

    private void setButtonred(int gasBtnNum) {
        Button[] buttons = new Button[]{gasBtn1, gasBtn2, gasBtn3, gasBtn4, gasBtn5, gasBtn6, gasBtn7, gasBtn8, gasBtn9, gasBtn10};
        Button button;
        for (int i = 0; i < gasBtnNum + 1; i++) {
            button = buttons[i];
            button.setBackgroundColor(getResources().getColor(R.color.mainred));
        }
    }

    private void setButtongrey() {
        Button[] buttons = new Button[]{gasBtn1, gasBtn2, gasBtn3, gasBtn4, gasBtn5, gasBtn6, gasBtn7, gasBtn8, gasBtn9, gasBtn10};
        Button button;
        for (int i = 0; i < 10; i++) {
            button = buttons[i];
            button.setBackgroundColor(getResources().getColor(R.color.imagebtngrey));
        }
    }

    private void initViews() {
        mToolbar = (Toolbar) findViewById(R.id.car_infor_toolbar);
        mToolbar.setTitle("基本信息采取");
        setSupportActionBar(mToolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);
        carKind = (MaterialEditText) findViewById(R.id.car_kind);
        carNumber = (TextView) findViewById(R.id.car_carnumber);
        carDistance = (MaterialEditText) findViewById(R.id.car_distance);
        carInforNextBtn = (Button) findViewById(R.id.car_infor_next_btn);
        carInforNextBtn.setOnClickListener(this);
        youzhenImg = (ImageView) findViewById(R.id.img_youzhen);
        RotateAnimation animation = new RotateAnimation(0f, -65f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 1.0f);
        animation.setFillAfter(true);
        animation.setDuration(1000);
        youzhenImg.startAnimation(animation);
        gasBtn1 = (Button) findViewById(R.id.gasbtn1);
        gasBtn1.setOnClickListener(this);
        gasBtn2 = (Button) findViewById(R.id.gasbtn2);
        gasBtn2.setOnClickListener(this);
        gasBtn3 = (Button) findViewById(R.id.gasbtn3);
        gasBtn3.setOnClickListener(this);
        gasBtn4 = (Button) findViewById(R.id.gasbtn4);
        gasBtn4.setOnClickListener(this);
        gasBtn5 = (Button) findViewById(R.id.gasbtn5);
        gasBtn5.setOnClickListener(this);
        gasBtn6 = (Button) findViewById(R.id.gasbtn6);
        gasBtn6.setOnClickListener(this);
        gasBtn7 = (Button) findViewById(R.id.gasbtn7);
        gasBtn7.setOnClickListener(this);
        gasBtn8 = (Button) findViewById(R.id.gasbtn8);
        gasBtn8.setOnClickListener(this);
        gasBtn9 = (Button) findViewById(R.id.gasbtn9);
        gasBtn9.setOnClickListener(this);
        gasBtn10 = (Button) findViewById(R.id.gasbtn10);
        gasBtn10.setOnClickListener(this);
        progressDialog = new ProgressDialog(this);
        progressDialog.setTitle("载入中");
        progressDialog.setMessage("请稍等");
        progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        progressDialog.setCancelable(true);
        progressUploadDialog = new ProgressDialog(this);
        progressUploadDialog.setTitle("上传中");
        progressUploadDialog.setMessage("请稍等");
        progressUploadDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        progressUploadDialog.setCancelable(true);
    }

    private void setbuttonAnima(int id) {
        setButtongrey();
        gasNum = id;
        setButtonred(id - 1);
        allAnimation = new RotateAnimation(point, -65f + 13f * id, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 1.0f);
        allAnimation.setFillAfter(true);
        allAnimation.setDuration(1500);
        youzhenImg.startAnimation(allAnimation);
        point = -65f + 13f * id;
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_car_infor, menu);
        return true;
    }

    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == android.R.id.home) {
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
